home *** CD-ROM | disk | FTP | other *** search
- /*
- * SnippitTest.c
- * Copyright © 1992-93, Apple Computer Inc. All Rights Reserved.
- * Programmed by Martin Minow,
- * Internet: minow@apple.com
- * AppleLink: MINOW
- * Ok, let's get one thing straight. We don't recommend that
- * you write applications in this sloppy fashion.
- */
- #include <OCE.h>
- #include <OCEAuthDir.h>
- #include <OCEErrors.h>
- #include <GestaltEqu.h>
- #include <Dialogs.h>
- #include <ToolUtils.h>
- #include <Resources.h>
- #include <Menus.h>
- #include <Errors.h>
- #include <Fonts.h>
- #include <Desk.h>
- #include <OSEvents.h>
- #include <Packages.h>
-
- #ifndef FALSE
- #define FALSE 0
- #define TRUE 1
- #endif
-
- WindowPtr gWindowPtr;
- #define WINDOW (gWindowPtr)
- EventRecord gEventRecord;
- #define EVENT (gEventRecord)
- short gLineHeight;
- short gLineAscent;
-
- void InitializeApplication(void);
- void MakeWindow(void);
- void EventLoop(void);
- void DoMouseEvent(void);
- void DoCommand(
- long menuChoice
- );
- void AdjustMenus(
- WindowPtr theWindow
- );
- void AdjustEditMenu(
- Boolean isDeskAcc
- );
- /*
- * Cheap 'n dirty pascal string copy routine.
- */
- #define pstrcpy(dst, src) do { \
- StringPtr _src = (src); \
- BlockMove(_src, dst, _src[0] + 1); \
- } while (0)
- /*
- * Cheap 'n dirty pascal string concat.
- */
- #define pstrcat(dst, src) do { \
- StringPtr _dst = (dst); \
- StringPtr _src = (src); \
- short _len; \
- _len = 255 - _dst[0]; \
- if (_len > _src[0]) _len = _src[0]; \
- BlockMove(&_src[1], &_dst[1] + _dst[0], _len); \
- _dst[0] += _len; \
- } while (0)
-
-
- enum {
- MENU_Apple = 1,
- MENU_File = 128,
- MENU_Edit = 129
- };
-
- enum AppleMenu {
- kAppleAbout = 1
- };
- enum FileMenu {
- kFileQuit = 1
- };
- #define kFileMenuString \
- "\pQuit/Q"
- enum EditMenu {
- kEditUndo = 1,
- kEditUnused,
- kEditCut,
- kEditCopy,
- kEditPaste,
- kEditClear
- };
- #define kEditMenuString \
- "\pUndo/Z;(-;Cut/X;Copy/C;Paste/V;Clear"
-
- MenuHandle gAppleMenu;
- MenuHandle gFileMenu;
- MenuHandle gEditMenu;
- Boolean gQuitNow;
-
- LocalIdentity gLocalIdentity;
- LocalIdentity gOldLocalIdentity;
- Boolean gSystemSupportsAOCE;
-
- enum {
- kLineHasAOCE = 0,
- kLineNotificationSetup,
- kLineAOCEUserID,
- kLastLine
- };
- Str255 gWindowContent[kLastLine];
-
- void TestSystemSupportsAOCE(void);
- void TestAuthNotificationSetup(void);
- void TestAuthNotification(void);
- void TestAuthNotificationRemoval(void);
- void TestGetUserIdentity(void);
- void ErrorMessage(
- OSErr errorStatus,
- short lineNumber,
- StringPtr message
- );
- void Message(
- short lineNumber,
- StringPtr message
- );
- void DrawWindowContent(void);
-
- OSErr SystemSupportsAOCE(void);
- OSErr GetUserIdentity(
- AuthIdentity *userIdentity
- );
- OSErr InitAuthNotificationQueue(
- LocalIdentity *localIdentityPtr
- );
- OSErr RemoveAuthNotificationQueue(void);
-
-
- void
- main(void)
- {
- InitializeApplication();
- MakeWindow();
- TestSystemSupportsAOCE();
- TestGetUserIdentity();
- TestAuthNotificationSetup();
- while (gQuitNow == FALSE) {
- EventLoop();
- TestAuthNotification();
- }
- TestAuthNotificationRemoval();
- ExitToShell();
- }
-
- void
- EventLoop(void)
- {
- long menuChoice;
- WindowPtr windowPtr;
- GrafPtr savePort;
- Boolean isActivate;
-
- WaitNextEvent(everyEvent, &EVENT, 10L, NULL);
- switch (EVENT.what) {
- case nullEvent:
- break;
- case keyDown:
- case autoKey:
- if ((EVENT.message & charCodeMask) == '.'
- && (EVENT.modifiers & cmdKey) != 0) {
- FlushEvents(keyDown | autoKey, 0);
- gQuitNow = TRUE;
- }
- else if ((EVENT.modifiers & cmdKey) != 0) {
- if (EVENT.what == keyDown) {
- menuChoice = MenuKey(EVENT.message & charCodeMask);
- if (HiWord(menuChoice) != 0)
- DoCommand(menuChoice);
- else {
- SysBeep(10);
- }
- }
- }
- else {
- SysBeep(10);
- }
- break;
- case mouseDown:
- DoMouseEvent();
- break;
- case activateEvt:
- windowPtr = (WindowPtr) EVENT.message;
- isActivate = (EVENT.modifiers & activeFlag) != 0;
- goto doActivate;
- break;
- case osEvt:
- switch (((unsigned long) EVENT.message) >> 24) {
- case mouseMovedMessage:
- break;
- case suspendResumeMessage:
- isActivate = (EVENT.message & resumeFlag) != 0;
- windowPtr = FrontWindow();
- doActivate: if (isActivate && windowPtr == WINDOW) {
- ShowWindow(windowPtr);
- SelectWindow(windowPtr);
- SetPort(windowPtr);
- InitCursor();
- }
- break;
- }
- break;
- case updateEvt:
- windowPtr = (WindowPtr) EVENT.message;
- if (windowPtr == WINDOW) {
- GetPort(&savePort);
- SetPort(windowPtr);
- BeginUpdate(windowPtr);
- EraseRect(&windowPtr->portRect);
- DrawControls(windowPtr);
- DrawWindowContent();
- EndUpdate(windowPtr);
- SetPort(savePort);
- }
- }
- }
-
- void
- DoMouseEvent(void)
- {
- WindowPtr theWindow;
- short whichPart;
-
- whichPart = FindWindow(EVENT.where, &theWindow);
- if (whichPart == inMenuBar && theWindow != WINDOW)
- theWindow = FrontWindow();
- AdjustMenus(theWindow);
- switch (whichPart) {
- case inDesk:
- SysBeep(10);
- break;
- case inMenuBar:
- InitCursor();
- DoCommand(MenuSelect(EVENT.where));
- break;
- case inContent:
- case inGoAway:
- case inDrag:
- SysBeep(10);
- break;
- }
- }
-
- void
- DoCommand(
- long menuChoice
- )
- {
- short menuItem;
- Str255 menuText;
- GrafPtr savePort;
-
- menuItem = LoWord(menuChoice);
- switch (HiWord(menuChoice)) {
- case MENU_Apple:
- if (menuItem != kAppleAbout) {
- GetItem(gAppleMenu, menuItem, menuText);
- AdjustEditMenu(TRUE);
- GetPort(&savePort);
- OpenDeskAcc(menuText);
- SetPort(savePort);
- AdjustEditMenu(FrontWindow() != WINDOW);
- }
- break;
- case MENU_File:
- /*
- * Note that File/Measure and File/Adjust are enabled
- * only if the application is in the idle state.
- */
- switch (menuItem) {
- case kFileQuit:
- gQuitNow = TRUE;
- break;
- default:
- SysBeep(10);
- break;
- }
- break;
- case MENU_Edit:
- if (SystemEdit(menuItem - 1) == FALSE)
- SysBeep(10);
- break;
- }
- HiliteMenu(0);
- }
-
- void
- AdjustMenus(
- WindowPtr theWindow
- )
- {
- EnableItem(gFileMenu, kFileQuit);
- AdjustEditMenu(theWindow != WINDOW);
- }
-
- void
- AdjustEditMenu(
- Boolean isDeskAcc
- )
- {
- if (isDeskAcc) {
- EnableItem(gEditMenu, kEditUndo);
- EnableItem(gEditMenu, kEditCut);
- EnableItem(gEditMenu, kEditCopy);
- EnableItem(gEditMenu, kEditPaste);
- EnableItem(gEditMenu, kEditClear);
- }
- else {
- DisableItem(gEditMenu, kEditUndo);
- DisableItem(gEditMenu, kEditCut);
- DisableItem(gEditMenu, kEditCopy);
- DisableItem(gEditMenu, kEditPaste);
- DisableItem(gEditMenu, kEditClear);
- }
- }
-
- void
- InitializeApplication(void)
- {
- int i;
-
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(NULL);
- SetCursor(*GetCursor(watchCursor));
- for (i = 0; i < 3; i++)
- EventAvail(everyEvent, &EVENT);
- gAppleMenu = NewMenu(MENU_Apple, "\p\024");
- AppendMenu(gAppleMenu, "\p(© 1992-93 Apple Computer Inc.;(-");
- AddResMenu(gAppleMenu, 'DRVR');
- InsertMenu(gAppleMenu, 0);
- gFileMenu = NewMenu(MENU_File, "\pFile");
- AppendMenu(gFileMenu, kFileMenuString);
- InsertMenu(gFileMenu, 0);
- gEditMenu = NewMenu(MENU_Edit, "\pEdit");
- AppendMenu(gEditMenu, kEditMenuString);
- InsertMenu(gEditMenu, 0);
- DrawMenuBar();
- InitCursor();
- }
-
- void
- MakeWindow(void)
- {
- Rect boundsRect;
- FontInfo info;
-
- SetRect(
- &boundsRect,
- 40, 40, 400, 300
- );
- WINDOW = NewWindow(
- NULL, /* Allocate window */
- &boundsRect,
- "\pSnippit Test",
- TRUE,
- plainDBox,
- (WindowPtr) -1L,
- FALSE, /* No go away box */
- 0
- );
- if (WINDOW == NULL) {
- SysBeep(10);
- SysBeep(10);
- ExitToShell();
- }
- SetPort(WINDOW);
- TextFont(geneva);
- TextSize(9);
- GetFontInfo(&info);
- gLineHeight = info.ascent + info.descent + info.leading;
- gLineAscent = info.ascent;
- }
-
- /*
- * Snippit test routines.
- */
- void
- TestSystemSupportsAOCE(void)
- {
- OSErr status;
-
- status = SystemSupportsAOCE();
- switch (status) {
- case noErr:
- Message(kLineHasAOCE, "\pAOCE is enabled");
- gSystemSupportsAOCE = TRUE;
- break;
- case kOCEToolboxNotOpen:
- Message(kLineHasAOCE, "\pAOCE is present, but disabled");
- gSystemSupportsAOCE = FALSE;
- break;
- case gestaltUndefSelectorErr:
- Message(kLineHasAOCE, "\pAOCE is not present on this machine");
- gSystemSupportsAOCE = FALSE;
- break;
- default:
- ErrorMessage(status, kLineHasAOCE, "\pTesting for AOCE presence");
- gSystemSupportsAOCE = FALSE;
- break;
- }
- }
-
- void
- TestGetUserIdentity(void)
- {
- OSErr status;
-
- if (gSystemSupportsAOCE == FALSE)
- Message(kLineAOCEUserID, "\pCan't test user identity");
- else {
- status = GetUserIdentity(&gLocalIdentity);
- switch (status) {
- case noErr:
- Message(kLineAOCEUserID, "\pGot user identity");
- break;
- case userCanceledErr:
- Message(kLineAOCEUserID, "\pUser canceled Get Identity");
- break;
- default:
- ErrorMessage(status, kLineAOCEUserID, "\pGet identity failed");
- break;
- }
- }
- }
-
- void
- TestAuthNotificationSetup(void)
- {
- OSErr status;
-
- if (gSystemSupportsAOCE == FALSE) {
- Message(
- kLineNotificationSetup,
- "\pCan't test identity notification queue"
- );
- }
- else {
- /*
- * Make sure that TestAuthNotification sees the change.
- */
- gOldLocalIdentity = (~gLocalIdentity);
- status = InitAuthNotificationQueue(&gLocalIdentity);
- switch (status) {
- case noErr:
- Message(
- kLineNotificationSetup,
- "\pIdentity notification queue initialized"
- );
- break;
- default:
- ErrorMessage(
- status,
- kLineNotificationSetup,
- "\pIdentity notification queue setup failed"
- );
- break;
- }
- }
- }
-
-
- void
- TestAuthNotification(void)
- {
- Str255 msg;
-
- if (gOldLocalIdentity != gLocalIdentity) {
- gOldLocalIdentity = gLocalIdentity;
- if (gOldLocalIdentity == 0)
- Message(kLineAOCEUserID, "\pAOCE Locked");
- else {
- NumToString(gLocalIdentity, msg);
- pstrcat(msg, "\p: current local identity");
- Message(kLineAOCEUserID, msg);
- }
- }
- }
-
- /*
- * Since this is called when the program exits, we can't
- * really test for error returns. Thus, we trap into DebugStr
- * on errors.
- */
- void
- TestAuthNotificationRemoval(void)
- {
- OSErr status;
-
- if (gSystemSupportsAOCE) {
- status = RemoveAuthNotificationQueue();
- if (status != noErr)
- DebugStr("\pRemoveAuthNotifyQueue error");
- }
- }
-
-
- void
- ErrorMessage(
- OSErr errorStatus,
- short lineNumber,
- StringPtr message
- )
- {
- Str15 errorNumber;
- Str255 msg;
-
- NumToString(errorStatus, errorNumber);
- pstrcpy(msg, "\pSystem error: ");
- pstrcat(msg, errorNumber);
- pstrcat(msg, message);
- Message(lineNumber, message);
- }
-
- void
- Message(
- short lineNumber,
- StringPtr message
- )
- {
- Rect boundsRect;
-
- SetPort(WINDOW);
- pstrcpy(gWindowContent[lineNumber], message);
- SetRect(
- &boundsRect,
- WINDOW->portRect.left,
- WINDOW->portRect.top + (lineNumber * gLineHeight) + 4,
- WINDOW->portRect.right,
- 0
- );
- boundsRect.bottom = boundsRect.top + gLineHeight;
- InvalRect(&boundsRect);
- }
-
- void
- DrawWindowContent(void)
- {
- short i;
-
- for (i = 0; i < kLastLine; i++) {
- MoveTo(
- 4,
- WINDOW->portRect.top
- + (i * gLineHeight)
- + 4
- + gLineAscent
- );
- DrawString(gWindowContent[i]);
- }
- }
-
-
-